<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <script src="../js/vue.js"></script>
    <link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
    <script src="../element-ui/lib/index.js"></script>
    <script src="../js/axios.js"></script>
    <style>
        .el-header {
            background-color: #409EFF;
            color: #FFFFFF;
            line-height: 60px;
        }

        .el-aside {
            color: #333;

        }
    </style>
</head>
<body>
<div id="app">
    <el-container style="height: 500px; border: 1px solid #eee">
        <el-header style="text-align:right;font-size: 12px">
            <el-dropdown>
                <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>个人信息</el-dropdown-item>
                    <el-dropdown-item>退出登录</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </el-header>
        <el-container>
            <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
                <el-menu :default-openeds="['1', '2']" default-active="1-1">
                    <el-submenu index="1">
                        <template slot="title"><i class="el-icon-user"></i>用户信息管理</template>
                        <el-menu-item-group title="基本信息">
                            <el-menu-item index="1-1">基本信息管理</el-menu-item>
                        </el-menu-item-group>
                        <el-menu-item-group title="订单管理">
                            <el-menu-item index="1-2">用户订单管理</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="2">
                        <template slot="title"><i class="el-icon-menu"></i>航班信息管理</template>
                        <el-menu-item-group>
                            <template slot="title">航班信息</template>
                            <el-menu-item index="2-1" @click="toManagePlane">航班信息管理</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>

                </el-menu>
            </el-aside>

            <el-main>
                <!--搜索表单-->
                <el-form :inline="true" :model="user" class="demo-form-inline">
                    <el-form-item label="姓名">
                        <el-input v-model="user.name" placeholder="姓名"></el-input>
                    </el-form-item>
                    <el-form-item label="性别">
                        <el-select v-model="user.sex" placeholder="性别">
                            <el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="手机号">
                        <el-input type="phone" v-model="user.phone" placeholder="手机号"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit" icon="el-icon-search">搜索</el-button>
                    </el-form-item>
                </el-form>
                <!--按钮 -->
                <el-row>
                    <el-button type="danger" @click="deleteByIds" plain>批量删除</el-button>
                    <el-button type="primary" @click="dialogVisible = true" plain>新增</el-button>
                </el-row>

                <!--添加数据对话框-->
                <el-dialog
                        title="编辑用户信息"
                        :visible.sync="dialogVisible"
                        width="30%">
                    <!--表单-->
                    <el-form ref="form" :model="form" label-width="80px">
                        <el-form-item label="姓名">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="性别" width="100%">
                            <el-select v-model="form.sex" placeholder="请选择">
                                <el-option label="男" value="男"></el-option>
                                <el-option label="女" value="女"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="联系方式">
                            <el-input type="number" v-model="form.phone"></el-input>
                        </el-form-item>
                        <el-form-item label="工作单位">
                            <el-input v-model="form.workplace"></el-input>
                        </el-form-item>
                        <el-form-item label="身份证号">
                            <el-input v-model="form.idCard"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-if="update==0" @click="addUser">提交</el-button>
                            <el-button type="primary" v-else @click="updateUser">确认修改</el-button>
                            <el-button @click="cancel">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
                <!--表格-->
                <el-table
                        v-loading="loading"
                        height="350"
                        :data="tableData"
                        style="width: 100%"
                        :row-class-name="tableRowClassName"
                        @selection-change="handleSelectionChange">
                    <el-table-column
                            type="selection"
                            width="55"
                            fixed
                    >
                    </el-table-column>
                    <el-table-column
                            type="index"
                            width="50"
                            align="center"
                            fixed
                    >
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="姓名"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="sex"
                            label="性别"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="phone"
                            label="联系方式"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="workplace"
                            label="工作地点"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="idCard"
                            label="身份证号"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="method"
                            label="操作"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-row>
                                <el-button type="primary" @click="handleUpdate(scope.$index, scope.row)" icon="el-icon-edit"></el-button>
                                <el-button  type="danger" @click="handleDelete(scope.$index, scope.row)" icon="el-icon-delete"></el-button>
                            </el-row>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页工具条    -->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="totalCount">
                </el-pagination>
            </el-main>

        </el-container>
    </el-container>
</div>
<script>
    new Vue({
        el: "#app",
        mounted() {
            this.selectAll()
        },
        methods: {
            cancel() {
                this.dialogVisible = false
                this.update = 0
                this.form = {
                    name: '',
                    sex: '',
                    phone: "",
                    workplace: '',
                    idCard: ""
                }
            },
            // selectAll(){
            //     axios({
            //         method: "get",
            //         url: "http://localhost/user"
            //     }).then((resp) => {
            //         console.log(resp)
            //         this.tableData = resp.data;
            //     })
            // },

            //查询分页数据
            selectAll() {
                this.loading = true
                axios({
                    method: "get",
                    url: "http://localhost/user/" + this.currentPage + "/" + this.pageSize
                }).then((resp) => {
                    //设置表格数据
                    this.tableData = resp.data.rows;
                    this.totalCount = resp.data.totalCount;

                    this.loading = false
                })
            },
            //分页条件查询
            selectByCondition() {
                this.loading = true

                // console.log(this.user)
                if (this.user.phone == '') {
                    this.user.phone = null
                }
                axios({
                    method: "post",
                    url: "http://localhost/user/" + this.currentPage + "/" + this.pageSize,
                    data: this.user
                }).then((resp) => {
                    console.log(resp)
                    //设置表格数据
                    this.tableData = resp.data.rows;
                    this.totalCount = resp.data.totalCount;
                    this.loading = false
                })
            },
            //每行类名
            tableRowClassName({row, rowIndex}) {
                if (rowIndex % 4 === 1) {
                    return 'warning-row';
                } else if (rowIndex % 4 === 3) {
                    return 'success-row';
                }
                return '';
            },

            //复选
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(val)
            },

            //搜索
            onSubmit() {
                this.currentPage = 1
                // console.log(this.user);
                this.selectByCondition()
            },
            //修改
            handleUpdate(index, row) {
                console.log(index, row);
                this.form = row
                this.update = 1
                this.dialogVisible = true

            },
            updateUser() {
                console.log(this.form)
                axios({
                    method: "put",
                    url: "http://localhost/user",
                    data: this.form
                }).then((resp) => {
                    if (resp.data == "success") {
                        this.update = 0
                        this.dialogVisible = false
                        this.selectAll()
                        this.$message({
                            message: "修改成功",
                            type: "success"
                        })
                    }
                })
                this.cancel()
            },
            //添加数据
            addUser() {
                console.log(this.form)
                axios({
                    method: "post",
                    url: "http://localhost/user",
                    data: this.form
                }).then((resp) => {
                    if (resp.data == "success") {
                        this.dialogVisible = false
                        this.selectAll()
                        this.$message({
                            message: "添加成功",
                            type: "success"
                        })
                    }
                })
                this.cancel()
            },

            //删除单个
            handleDelete(index, row) {
                console.log(index, row);
                //弹出操作对话框
                this.$confirm('此操作将删除选择的数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios({
                        method: "delete",
                        url: "http://localhost/user/" + row.id,
                    }).then((resp) => {
                        if (resp.data == "success") {
                            this.selectAll()
                            this.$message({
                                message: "删除成功",
                                type: "success"
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },

            //批量删除
            deleteByIds() {
                //弹出操作对话框
                this.$confirm('此操作将删除选择的数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // console.log(this.multipleSelection)
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        this.selectedIds[i] = this.multipleSelection[i].id;
                    }
                    // console.log(this.selectedIds)
                    axios({
                        method: "delete",
                        url: "http://localhost/user",
                        data: this.selectedIds
                    }).then((resp) => {
                        if (resp.data == "success") {
                            this.selectAll()
                            this.$message({
                                message: "删除成功",
                                type: "success"
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },

            //分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.selectByCondition()
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.currentPage = val
                this.selectByCondition()
            },
            toManagePlane(){
                window.location.href="manager_plane.html"
            }
        },
        data() {
            return {
                //加载
                loading: true,
                //搜索
                user: {
                    name: null,
                    sex: null,
                    phone: null,
                },
                //对话框是否可见
                dialogVisible: false,
                update: 0,
                form: {
                    name: '',
                    sex: '',
                    phone: "",
                    workplace: '',
                    idCard: ""
                },
                selectedIds: [],
                //复选框选中数据集合
                multipleSelection: [],
                //表格数据
                tableData: [],
                totalCount: '',
                currentPage: 1,
                //每页显示条数
                pageSize: 5
            }
        }
    })
</script>
</body>
</html>